package com.xunlai.infra.permission.manager.web.controller;

import com.xunlai.infra.permission.common.exception.PermissionBusinessException;
import com.xunlai.infra.permission.manager.web.service.PositionService;
import com.xunlai.infra.permission.manager.web.vo.BasePageRespVo;
import com.xunlai.infra.permission.manager.web.vo.BaseResponse;
import com.xunlai.infra.permission.manager.web.vo.position.CreatePositionReqVo;
import com.xunlai.infra.permission.manager.web.vo.position.PositionListVo;
import com.xunlai.infra.permission.manager.web.vo.position.SearchPositionReqVo;
import com.xunlai.infra.permission.manager.web.vo.position.UpdatePositionReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author liang
 * @date 12/21/22 9:30 AM
 */
@PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
@RestController
@RequestMapping("system/position")
public class PositionController {

    private PositionService positionService;


    @PostMapping("create")
    public BaseResponse<Long> create(@RequestBody @Validated({CreatePositionReqVo.ValidCreatePositionGroup.class}) CreatePositionReqVo createPositionReqVo) {
        try {
            return BaseResponse.success(positionService.createPosition(createPositionReqVo));
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(), e.getMsg());
        }
    }

    @PutMapping("update")
    public BaseResponse<Void> update(@RequestBody @Validated({UpdatePositionReqVo.ValidUpdatePositionGroup.class}) UpdatePositionReqVo updatePositionReqVo) {
        try {
            positionService.updatePosition(updatePositionReqVo);
            return BaseResponse.success();
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(), e.getMsg());
        }
    }

    @DeleteMapping("/{id}")
    public BaseResponse<Void> delete(@PathVariable Long id, @RequestParam(required = false, defaultValue = "false") Boolean force) {
        try {
            positionService.deletePosition(id, force);
            return BaseResponse.success();
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(), e.getMsg());
        }
    }

    @GetMapping("list")
    public BaseResponse<BasePageRespVo<PositionListVo>> list(SearchPositionReqVo searchPositionReqVo,
                                                             @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return BaseResponse.success(positionService.pageListPosition(searchPositionReqVo,pageNum,pageSize));
    }

    @Autowired
    public void setPositionService(PositionService positionService) {
        this.positionService = positionService;
    }
}
